require(['grid', 'topBar', 'layout','util','validator'], function () {
    var editDialog,funName, $top = $('#top'),$grid = $('#grid'),reloadGridData,corpId;
    //左侧树
	commonFun.leftTreeNodeClickObj['funFaultSet'] = function (event, treeId, treeNode) {
		if(treeNode.type==='LINE'){
			corpId=treeNode.corpId;
		}else{
			$.messager.alert("提示","请在左侧树中选择线路，再点击查询。","info");
		}
	};
    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '故障编码',
                    name: 'mftTypeCodeSearch',
                    width: 80,
                    type: 'text'
                },
                {
                    text: '故障名称',
                    name: 'mftTypeNameSearch',
                    width: 80,
                    type: 'text'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	reloadGridData();
                    }
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                }
                ,
                /*{
                    text: '+新增故障',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	if(commonFun.getLeftTreeSelectedNodes()!=""){
                			getEditDialog('add','新增').dialog('exec', 'reset',commonFun.getLeftTreeSelectedNodes()[0].corpId);
                		}else{
                			$.messager.alert("提示","请在左侧树中选择线路，再点击查询。","info");
                		}
                    }
                }*/
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/faultType/searchFaultTypePage.htm",
            colModel: [
                {name: "rowidstr",  label: 'rowidstr', key: true, hidden: true},
                {name: "mftTypeCode",  label: '故障编码'},
                {name: "mftDiscCode",  label: '故障区分代码'},
                {name: "mftTypeName",  label: '故障名称'},
                {name: "ecuMftCode", label: 'ECU故障编码'},
                {name: "ecuMftCode", label: 'ECU故障名称',dict:"ECU00000"},
                {name: "mftClCode", label: '故障分类代码',dict:"MFTC0000"},
                {name: "mftGradeCode", label: '故障等级编码'},
                {name: "mftGradeCode", label: '故障等级名称',dict: "FAL00000"},
                {name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '查看',
                        css:'operate-edit',
                        click: function (data) {
                        	 getEditDialog('edit','查看').dialog('exec', 'setVal',data.rowidstr);
                        }
                    }
                   /* , {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                            $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                                if (r) {
                                	$.ajaxExt({
            							url : "/faultType/delFaultType.htm",
            							data : {'mftTypeCode' : data.mftTypeCode}
            						}).done(function(){
            							reload();
            						});
                                }
                            });
                        }
                    }*/
                    ]
                }
            ],
            multiselect: false
        });
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(flag,option) {
    	funName = flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '故障设置维护',
                url: './safety/setting/funFaultSetEdit.htm',
                height: 550,
                width: 720,
                autoOpen: true
               /* ,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	var _t = this;
                        	 //funName :  add or edit
                        	 $(_t).dialog('exec', funName,function(){
                 		    	reload(true);
                 		    });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]*/
            });
        }
        return editDialog;
    }

    //查询grid
	reloadGridData = function(){
		if(commonFun.getLeftTreeSelectedNodes()!=""){
			corpId=commonFun.getLeftTreeSelectedNodes()[0].corpId;
			var postData = {};
		    postData = $top.getValues();
		    postData.corpId = corpId;
			$grid.grid('setGridParam', {
		        datatype: "JSON",
		        page: 1,
		        postData: postData
		    }).trigger("reloadGrid");
		}else{
			$.messager.alert("提示","请在左侧树中选择线路，再点击查询。","info");
		}
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			corpId=commonFun.getLeftTreeSelectedNodes()[0].corpId;
			var postData = {};
		    postData = $top.getValues();
		    postData.corpId = corpId;
    		$.exportFile("/faultType/exportFaultType"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	 
	//新增修改后执行
	function reload(close){
    	reloadGridData();
    	close && editDialog.dialog('close');
        commonFun.reloadLeftTree();
	};
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});